package com.mark.web.data.controller.portal;

import com.google.common.base.Splitter;
import com.mark.common.CurrentUser;
import com.mark.common.ServerResponse;
import com.mark.web.data.entity.vo.CartVo;
import com.mark.web.data.enums.CartEnum;
import com.mark.web.data.service.ICartService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Description: 购物车控制器
 * @Author: Mark
 * @CreateDate: 2018/8/14.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@RestController
@RequestMapping("/cart/")
public class CartController {

    private final ICartService iCartService;

    public CartController(ICartService iCartService) {
        this.iCartService = iCartService;
    }

    /**
     * 1.查看购物
     * @return  购物车列表信息
     */
    @ApiOperation(value = "查看购物车列表")
    @GetMapping(value = "cart_list.do")
    public ServerResponse getCartList(){
        int currentUserId = CurrentUser.getCurrentUserId();
        return iCartService.getCartList(currentUserId);
    }


    /**
     * 2.添加商品到购物车
     *
     * @param productId  :添加到购物车商品id
     * @param count：添加到购物车的商品的数量
     * @return ： 加入购物的结果（成功，失败[库存量不足]）
     */
    @ApiOperation(value = "加入购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId",value = "商品id",required = true,dataType = "int"),
            @ApiImplicitParam(name = "count",value = "商品数量",required = true,dataType = "int")
    })
    @PostMapping(value = "add_list.do")
    public ServerResponse add(Integer productId, Integer count) {

        if (productId < 0 || count < 0) {
            return ServerResponse.createByIllegalArgument("输入的商品id和数量的参数值不合法！");
        }
        return iCartService.addProductToCart(CurrentUser.getCurrentUserId(), productId, count);
    }

    /**
     * 3.更新购物车
     *
     * @param count ：更新后的商品数量
     * @param productId ： 待更新的商品ID
     * @return
     */
    @ApiOperation(value = "更新购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId",value = "商品id",required = true,dataType = "int"),
            @ApiImplicitParam(name = "count",value = "商品数量",required = true,dataType = "int")
    })
    @PostMapping(value = "update.do")
    public ServerResponse<CartVo> update(Integer count, Integer productId){
        if(productId < 0 || count < 0){
            return ServerResponse.createByIllegalArgument("输入的商品id和数量的参数值不合法！");
        }
        return iCartService.updateCart(CurrentUser.getCurrentUserId(),productId,count);
    }


    /**
     * 删除购物车中的商品
     *
     * @param productIds：以逗号分隔的商品id字符串
     * @return
     */
    @ApiOperation(value = "移除商品")
    @ApiImplicitParam(name = "productIds",value = "以逗号分隔的商品id字符串",required = true,dataType = "string")
    @PostMapping(value = "delete.do")
    public ServerResponse<CartVo> deleteProduct(String productIds){
        List<String> productList = Splitter.on(",").splitToList(productIds);
        if (CollectionUtils.isEmpty(productList)) {
            return ServerResponse.createByIllegalArgument("输入的参数值不合法！");
        }
        return iCartService.deleteProduct(CurrentUser.getCurrentUserId(),productList);
    }

    /**
     * 购物车商品的全选
     * @return
     */
    @ApiOperation(value = "商品的全选")
    @PostMapping(value = "select_all.do")
    public ServerResponse<CartVo> selectAll(){
        return iCartService.selectOrUnSelect(CurrentUser.getCurrentUserId(),null, CartEnum.CHECKED.getState());
    }

    /**
     * 购物车的商品全反选
     * @return
     */
    @ApiOperation(value = "商品的全反选")
    @PostMapping(value = "un_select_all.do")
    public ServerResponse<CartVo> unSelectAll(){
        return iCartService.selectOrUnSelect(CurrentUser.getCurrentUserId(),null, CartEnum.UN_CHECKED.getState());
    }

    /**
     * 购物车中商品的单选
     * @param productId：商品id
     * @return
     */
    @ApiOperation(value = "商品的单选")
    @ApiImplicitParam(name = "productId",value = "商品ID",required = true,dataType = "int")
    @PostMapping(value = "select.do")
    public ServerResponse<CartVo> select(Integer productId){
        return iCartService.selectOrUnSelect(CurrentUser.getCurrentUserId(), productId, CartEnum.CHECKED.getState());
    }

    /**
     * 购物车中商品的单反选
     * @param productId：商品id
     * @return
     */
    @ApiOperation(value = "商品的单反选")
    @ApiImplicitParam(name = "productId",value = "商品ID",required = true,dataType = "int")
    @PostMapping(value = "un_select.do")
    public ServerResponse<CartVo> unSelect(Integer productId){
        if(productId < 0){
            return ServerResponse.createByIllegalArgument("输入的参数值不合法");
        }
        return iCartService.selectOrUnSelect(CurrentUser.getCurrentUserId(), productId, CartEnum.UN_CHECKED.getState());
    }

    /**
     * 9，获取购物车商品的数量
     *
     * @return ：获取当前用户的购物车中的商品数量
     */
    @GetMapping(value = "get_cart_product_count.do")
    public ServerResponse<Integer> getCartProductCount(){
        return iCartService.getCartProductCount(CurrentUser.getCurrentUserId());
    }
}
